<!--
@Time:2021/11/30 21:21
@File:backend.py
@Author:常雷
@Software:PyCharm
-->
{% extends 'backend/back_base.html' %}

{% block article %}
    {# 标题   发布状态 评论数 阅读数 操作#}
    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th>标题</th>
                <th>发布状态</th>
                <th>评论数</th>
                <th>点赞数</th>
                <th>操作</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for article in page_queryset %}
                <tr>

                    <td><a href="/{{ request.user.username }}/article/{{ article.pk }}" class="title">{{ article.title }} ({{ article.create_time|date:'Y-m-d' }})</a></td>
                    <td>已发布</td>
                    <td>{{ article.comment_num }}</td>
                    <td>{{ article.up_num }}</td>
                    <td><a href="/edit_article/" class="id_edit" username="{{ request.user.username }}"  blog="{{ article.blog }}">编辑</a></td>
                    <td><a href="" class="id_del" username="{{ request.user.username }}"  article_pk="{{ article.pk }}">删除</a></td>
                </tr>
            {% endfor %}

        </tbody>
    </table>
    <div class="pull-right">
        {{ page_obj.page_html|safe }}
    </div>
{% endblock %}

{% block js %}
    <script>
        //编辑
        $('.id_edit').click(function (){
            $.ajax({
                url:'/set_password/',
                type:'post',
                data:{
                    'old_password':$('#id_old_password').val(),
                    'new_password':$('#id_new_password').val(),
                    'confirm_password':$('#id_confirm_password').val(),
                    'csrfmiddlewaretoken':'{{ csrf_token }}'
                },
                success:function (args){
                    if (args.code === 1000){
                        window.location.reload()
                    }else{
                        $('#id_edit_error').text(args.msg)
                    }
                }
            })
        });
        //删除
        $('.id_del').click(function (){
            let username = $(this).attr('username');
            let article_pk = $(this).attr('article_pk');
            if (confirm("要删除 {{ username }} 的这篇文章吗?")) {
                $.ajax({
                    url:'/del_article/',
                    type:'post',
                    data:{
                        'username':username,
                        'article_pk':article_pk,
                        'csrfmiddlewaretoken':'{{ csrf_token }}'
                    },
                    success:function (args){
                        if (args.code === 1000){
                            window.location.reload()
                        }else{
                            alert('错误! 删除失败!')
                        }
                    }
                })
            }
        })
    </script>
{% endblock %}